<template>
  <div class="nav-bar-container">
    <el-row :gutter="15">
      <el-col :xs="4" :sm="12" :md="12" :lg="12" :xl="12">
        <div class="left-panel">
          <i
            :class="collapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"
            :title="collapse ? '展开' : '收起'"
            class="fold-unfold"
            @click="handleCollapse"
          ></i>
          <vab-icon :icon="['fas', 'map-marker-alt']"></vab-icon>
          <vab-breadcrumb
            class="hidden-xs-only"
            :style="{ display: $route.name != 'Index' ? 'block' : 'none' }"
          />
          <div
            class="changepage"
            :style="{ display: $route.name == 'Index' ? 'block' : 'none' }"
          >
            <span
              :class="showIndex == 0 ? 'btnactive' : ''"
              @click="showpage(0)"
            >
              首页
            </span>
            <span
              :class="showIndex == 1 ? 'btnactive' : ''"
              @click="showpage(1)"
            >
              货位页
            </span>
          </div>
        </div>
      </el-col>
      <el-col :xs="20" :sm="12" :md="12" :lg="12" :xl="12">
        <div class="right-panel">
          <!-- <vab-error-log /> -->
          <span class="top-time" v-show="wenduShow">
            温度：{{ argswendu }} ℃ 湿度：{{ argsshidu }} %
          </span>
          <span class="top-time">{{ $util.getNowFormatDate() }}</span>
          <!-- <vab-full-screen-bar @refresh="refreshRoute" />
          <vab-theme-bar class="hidden-xs-only" />
          <vab-icon
            title="重载所有路由"
            :pulse="pulse"
            :icon="['fas', 'redo']"
            @click="refreshRoute"
            style="color: #fff"
          /> -->
          <vab-avatar />
          <!--  <vab-icon
            title="退出系统"
            :icon="['fas', 'sign-out-alt']"
            @click="logout"
          />-->
        </div>
      </el-col>
    </el-row>
    <div
      :class="
        collapse && $route.name == 'Index'
          ? 'index-title'
          : 'index-title-animation'
      "
    >
      <span>
        <img src="../../../assets/bayi.png" alt="" />
        外挂库管理系统
      </span>
    </div>
  </div>
</template>

<script>
  import { mapActions, mapGetters } from 'vuex'
  import axios from 'axios'
  export default {
    name: 'VabNavBar',
    data() {
      return {
        pulse: false,
        wenduList: [],
        argswendu: '',
        argsshidu: '',
        wenduShow: true,
      }
    },
    watch: {
      $route: {
        handler: function (val, oldVal) {
          if (val.name == 'Index') {
            this.foldSideBar()
          }
        },
        // 深度观察监听
        deep: true,
      },
    },
    computed: {
      ...mapGetters({
        collapse: 'settings/collapse',
        visitedRoutes: 'tabsBar/visitedRoutes',
        device: 'settings/device',
        routes: 'routes/routes',
        showIndex: 'settings/showIndex',
      }),
    },
    async mounted() {
      await this.getWenDuList()
      setInterval(async () => {
        await this.getWenDuList()
      }, 1000 * 60)
      if (this.$route.name == 'Index') {
        this.foldSideBar()
      }
    },
    methods: {
      ...mapActions({
        changeCollapse: 'settings/changeCollapse',
        foldSideBar: 'settings/foldSideBar',
        changeShowIndex: 'settings/changeShowIndex',
      }),

      async getWenDuList() {
        // this.wenduList = Object.values(result.data)[0]
        // let wenduAll = []
        // let shiduAll = []
        // this.wenduList.forEach((item) => {
        //   if (item.tem != 0) {
        //     wenduAll.push(item.tem)
        //   }
        // })
        // this.wenduList.filter((item) => {
        //   if (item.hum != 0) {
        //     shiduAll.push(item.hum)
        //   }
        // })
        // var sum = function (x, y) {
        //   return x + y
        // }
        // if (wenduAll.length > 0) {
        //   this.argswendu = (wenduAll.reduce(sum) / wenduAll.length).toFixed(1)
        //   this.argsshidu = (shiduAll.reduce(sum) / shiduAll.length).toFixed(1)
        // }
        await axios({
          headers: {
            'Content-Type': 'application/json',
            token: localStorage.getItem('token'),
          },
          method: 'get',
          url: `http://192.168.1.3:8091/wenshiduApi/getWenshidu`,
        }).then((res) => {
          if (res.data.code == 1) {
            this.wenduShow = false
          } else {
            this.argswendu = res.data.data.tem
            this.argsshidu = res.data.data.hum
            this.wenduShow = true
          }

          // this.wenduList = Object.values(res.data)[0]

          // let wenduAll = []
          // let shiduAll = []
          // this.wenduList.forEach((item) => {
          //   if (item.tem != 0) {
          //     wenduAll.push(item.tem)
          //   }
          // })
          // this.wenduList.forEach((item) => {
          //   if (item.hum != 0) {
          //     shiduAll.push(item.hum)
          //   }
          // })
          // var sum = function (x, y) {
          //   return x + y
          // }
          // if (wenduAll.length > 0) {
          //   this.argswendu = (wenduAll.reduce(sum) / wenduAll.length).toFixed(1)
          //   this.argsshidu = (shiduAll.reduce(sum) / shiduAll.length).toFixed(1)
          // }
        })
      }, //改变展示页
      showpage(data) {
        this.changeShowIndex(data)
      },
      handleCollapse() {
        this.changeCollapse()
      },
      async refreshRoute() {
        this.$baseEventBus.$emit('reload-router-view')
        this.pulse = true
        setTimeout(() => {
          this.pulse = false
        }, 1000)
      },
    },
  }
</script>

<style lang="scss" scoped>
  .nav-bar-container {
    position: relative;
    height: $base-nav-bar-height;
    padding-right: $base-padding;
    padding-left: $base-padding;
    overflow: hidden;
    user-select: none;
    background: $navbar-color-blue;
    box-shadow: $base-box-shadow;

    .index-title {
      position: fixed;
      width: 100%;
      height: $base-nav-bar-height;
      background-color: $navbar-color-blue;
      top: 0;
      right: 0;
      background-image: url('../../../assets/head.png');
      background-repeat: no-repeat;
      background-size: 100% 100%;
      font-size: 0.28rem;
      color: #fff;
      line-height: $base-nav-bar-height;
      text-align: center;

      span {
        display: flex;
        /* padding: 50px; */
        align-content: center;
        justify-content: center;
        height: 0.5rem;
        line-height: 0.5rem;

        img {
          object-fit: scale-down;
        }
      }
    }

    .index-title-animation {
      position: fixed;
      height: $base-nav-bar-height;
      background-color: $navbar-color-blue;
      width: 0;
      top: 0;
      right: 0;
      background-image: url('../../../assets/head.png');
      background-repeat: no-repeat;
      background-size: 100% 100%;
      font-size: 0.28rem;
      color: #fff;
      line-height: $base-nav-bar-height;
      text-align: center;
      animation: 0.3s in-out forwards;
    }

    @keyframes in-out {
      0% {
        width: 100%;
        opacity: 1;
      }

      100% {
        width: 0;
        opacity: 0;
      }
    }

    .left-panel {
      display: flex;
      align-items: center;
      justify-items: center;
      height: $base-nav-bar-height;
      position: fixed;
      z-index: 2;

      .fold-unfold {
        color: #fff;
        cursor: pointer;
        font-size: 0.18rem;
      }

      ::v-deep {
        .breadcrumb-container {
          margin-left: 0.1rem;
        }
      }
    }

    .right-panel {
      display: flex;
      align-content: center;
      align-items: center;
      justify-content: flex-end;
      height: $base-nav-bar-height;
      position: fixed;
      z-index: 2;
      right: 0.2rem;

      ::v-deep {
        svg {
          width: 1em;
          height: 1em;
          margin-right: 0.15rem;
          font-size: $base-font-size-small;
          color: $base-color-gray;
          cursor: pointer;
          fill: $base-color-gray;
        }

        button {
          svg {
            margin-right: 0;
            color: $base-color-white;
            cursor: pointer;
            fill: $base-color-white;
          }
        }

        .el-badge {
          margin-right: 0.15rem;
        }
      }

      .top-time {
        color: $base-color-white;
        font-size: 0.2rem;
        margin-right: 0.15rem;
      }
    }
  }

  .changepage {
    span {
      display: inline-block;
      line-height: 0.4rem;
      text-align: center;
      width: 1rem;
      height: 0.4rem;
      background: #1a4d8d;
      color: #bfe6ff;
      border: none;
      box-shadow: 1px 1px 2px 1px #999;
      -webkit-clip-path: polygon(0 0, 75% 0, 100% 100%, 25% 100%);
      clip-path: polygon(0 0, 75% 0, 100% 100%, 25% 100%);
      margin-top: -0.06rem;
    }

    .btnactive {
      color: #fff;
      background: linear-gradient(
        180deg,
        rgba(0, 35, 73, 0.57) 2.944%,
        rgb(0, 144, 220) 100%
      ) !important;
    }

    span:first-child {
      margin-left: 0.4rem;
    }

    span {
      margin-left: -0.2rem;
    }
  }
</style>
